program define destpoint
    
    args dlat1 dlon1 d b lat2 lon2
    
    local R 6371
    
    tempname lat1 lon1 brng
    gen double `lat1' = `dlat1' * _pi / 180
    gen double `lon1' = `dlon1' * _pi / 180
    gen double `brng' = `b' * _pi / 180
    
    gen double `lat2' = asin(sin(`lat1') * cos(`d'/`R') + ///
                        cos(`lat1') * sin(`d'/`R') * cos(`brng'))
                        
    gen double `lon2' = `lon1' + atan2(sin(`brng') * sin(`d'/`R') * cos(`lat1'), ///
                                        cos(`d'/`R') - sin(`lat1') * sin(`lat2'))
                                        
    qui replace `lat2' = `lat2' * 180 / _pi
    qui replace `lon2' = `lon2' * 180 / _pi
    
end

clear
set obs 360 
gen nid = _n

local dtarget 12

gen double clat = 33.695163
gen double clon = -80.210913
gen double bearing = _n-1

destpoint clat clon `dtarget' bearing nlat nlon

expand 2 if _n==1 | _n==_N
sort nid
replace nlat = nlat[1] in l
replace nlon = nlon[1] in l
replace nlat = . in 1
replace nlon = . in 1
    
keep nlat nlon
rename nlat _Y
rename nlon _X
gen _ID = 1
order _ID _X _Y
save "V:/Health_IT/Urgent_Care/R2/manning_radius.dta", replace

clear
set obs 360 
gen nid = _n

local dtarget 16

gen double clat = 33.695163
gen double clon = -80.210913
gen double bearing = _n-1

destpoint clat clon `dtarget' bearing nlat nlon

expand 2 if _n==1 | _n==_N
sort nid
replace nlat = nlat[1] in l
replace nlon = nlon[1] in l
replace nlat = . in 1
replace nlon = . in 1
    
keep nlat nlon
rename nlat _Y
rename nlon _X
gen _ID = 1
order _ID _X _Y
save "V:/Health_IT/Urgent_Care/R2/manning_radius_v2.dta", replace

clear
set obs 360 
gen nid = _n

local dtarget 21

gen double clat = 33.695163
gen double clon = -80.210913
gen double bearing = _n-1

destpoint clat clon `dtarget' bearing nlat nlon

expand 2 if _n==1 | _n==_N
sort nid
replace nlat = nlat[1] in l
replace nlon = nlon[1] in l
replace nlat = . in 1
replace nlon = . in 1
    
keep nlat nlon
rename nlat _Y
rename nlon _X
gen _ID = 1
order _ID _X _Y
save "V:/Health_IT/Urgent_Care/R2/manning_radius_v3.dta", replace